package br.ufjf.posjava.cm.util;

import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author Filipe
 */
public class TrataDatas {

    private Object Data;
    private Object hora;

    /**
     * Creates a new instance of TrataDatas
     */
    public TrataDatas() {
    }

    public static boolean dataInvalida(String data) {
        try {
            String[] dt = data.split("/");
            int ano = Integer.parseInt(dt[2]);
            int mes = Integer.parseInt(dt[1]);
            int dia = Integer.parseInt(dt[0]);
            if ((dia < 1) || (dia > 31)) {
                return true;
            }
            if (((dia == 30) || (dia == 31)) && (mes == 2)) {
                return true;
            }
            if ((dia == 31) && ((mes == 2) || (mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))) {
                return true;
            }
            if ((mes < 1) || (mes > 12)) {
                return true;
            }
            if ((dia == 29) && (mes == 2) && (new GregorianCalendar().isLeapYear(ano) == false)) {
                return true;
            }
        } catch (Exception e) {
            return true;
        }
        return false;
    }

    public static Date strToDate(String data) throws ParseException {
        if (data == null) {
            return null;
        }
        java.util.Date dataF;
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        long timestamp = dateFormat.parse(data).getTime();
        dataF = new java.util.Date(timestamp);
        return dataF;
    }

    public static String dateToStr(Date data) throws ParseException {
        if (data == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        return dateFormat.format(data);
    }
}
